var ns;
app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        showtime:false,
        ProjectName:'',
        projectList: [], 
        list: [],
        form: {
            pageindex: 0,
            pagesize: 10,
            startTime: '',
            endTime: '',
            activeDate: 0,
            activeDateDesc: '时间',
            ProjectCompanyType: 1,
            ProjectID:0,
            ProjectIDs: [],
            StatusIDs:[],
        },
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        showTop:false,
        showsearch:false,
        StatusList:[
            {"ID":0,"Name":"全部"},
            {"ID":1,"Name":"未开始"},
            {"ID":2,"Name":"执行中"},
            {"ID":3,"Name":"已完成"},
            {"ID":4,"Name":"已结束"},
        ],
        IsAnalysis:false,//延期 
        TaskEndTime:'', 
        IsAllowDelay:false,
        DelayID:0,
        DelayTime:'',
        DelayTimeDiff:30,
        timeModel: {
            ID: 1,
            time: 30
        },
        timelist:[
            {ID:1,time:30},
            {ID:2,time:60},
            {ID:3,time:90},
        ],
        IsDelayTime:false,
        minDate: new Date(),
        maxDate: new Date(),
        currentDate:0,
        IsAnalysis:false,
    },
    methods: {
        doSelectProject: function(ID,Name) {
            var that = this;
            that.ProjectName=Name;
            that.form.ProjectID=ID;
            that.form.ProjectIDs=[];
            that.form.ProjectIDs.push(ID);
        },
        doSelectStatus: function (ID) {
            var that = this;
            var index = that.form.StatusIDs.indexOf(ID);
            if (index > -1) {
                that.form.StatusIDs.splice(index, 1);
            } else {
                that.form.StatusIDs.push(ID);
            }
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            if (type == 2 && that.showsearchmonth) {
                setTimeout(function () {
                    that.initPicker();
                }, 100);
            }
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        doSearch: function () {
            var that = this;
            that.doHideSearch();
            that.get_data();
        },
         //筛选数据
         getSearchParams: function () {
            var that = this;
            var options = {};
            options.action = "gettaskanalysisparams";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.projectList = data.projectList;
                    if(that.projectList!=undefined&&that.projectList.length>0&&that.form.ProjectID<=0){
                        that.doSelectProject(that.projectList[0].ID,that.projectList[0].Name); 
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        get_data: function() {
            var that = this;
            that.is_searching= true,
            that.can_scroll = false;
            var options = {};
            options.action = "getanalysisequipmentlist";
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.PlanType = 4;
            options.projectIds = JSON.stringify(that.form.ProjectIDs);
            options.startTime = that.form.startTime;
            options.endTime = that.form.endTime;
            options.statusIds = JSON.stringify(that.form.StatusIDs);
            options.IsAnalysis=that.IsAnalysis;
            ns.post(options, function(succeed, data, err) {
                that.is_searching= false;
                if (succeed) {
                    if (data.list.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.list;

                    } else {
                        that.list = that.list.concat(data.list);
                    }
                    app.current_item_length = that.list.length
                    
                } else if (err) {
                    // that.list = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        open_list: function(item) {
            var that = this;
            // var url = 'bjlh_list_frm_task';
            var url = 'bjlh_list_frm';
            var title = item.DevicePlanName;
            ns.openWin(url, title, {
                id: item.ID,
                TaskType:1,
            });
        },
        doClickDay: function(dataValue) {
            var that = this;
            that.form.activeDate = dataValue;
            var startDate = '',
                endDate = '';
            if (dataValue == 1) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate = dateRangeUtil.getDateString(currentDate);
                endDate = startDate;
                that.form.activeDateDesc = '今日';
            }
            if (dataValue == 2) {
                var currentWeek = dateRangeUtil.getCurrentWeek();
                startDate = dateRangeUtil.getDateString(currentWeek[0]);
                endDate = dateRangeUtil.getDateString(currentWeek[1]);
                that.form.activeDateDesc = '本周';
            }
            if (dataValue == 3) {
                var currentMonth = dateRangeUtil.getCurrentMonth();
                startDate = dateRangeUtil.getDateString(currentMonth[0]);
                endDate = dateRangeUtil.getDateString(currentMonth[1]);
                that.form.activeDateDesc = '本月';
            }
            that.form.startTime = startDate;
            that.form.endTime = endDate;
        },
        doReset: function(type) {
            var that = this;
            if (type == 2) {
                that.form.startTime = '';
                that.form.endTime = '';
                that.form.activeDate = 0;
                that.form.activeDateDesc='时间';
            } else {
                that.form.StatusIDs = [];
                that.form.ProjectIDs = [];
            }
        },
        //延期
        open_detail_delay:function(item){
            var that=this;
            that.DelayID=item.ID;
            that.TaskEndTime=item.ETime;//这个时间要年月日时分的
            app.minDate=new Date(moment(new Date(that.TaskEndTime)));
            app.maxDate=new Date(moment(new Date()).add(50, 'y'));
            that.onChooseDelayTime(that.timelist[0]);
            that.IsAllowDelay=true;
        },
        open_ok_delay:function(){
            var that=this;
            var options = {};
            options.action = "environment_clean_plan_addtime";
            options.PlanTaskId = that.DelayID;
            options.EndTime = that.DelayTime;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if(data.status){
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.open_cancel_delay();
                        that.doSearch();//刷新列表
                    }
                    else{
                        if(data.Msg){
                            api.toast({
                                msg: data.Msg,
                                duration: 2000,
                                location: 'bottom'
                            });
                            return;
                        }
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
            }, {
                toast: true
            });
        },
        open_cancel_delay:function(){
            var that=this;
            that.DelayID=0;
            that.IsAllowDelay=false;
        },
        onChooseDelayTime(item){
            var that=this;
            that.timeModel=item;
            that.DelayTimeDiff=item.time;
            that.DelayTime=moment(new Date(that.TaskEndTime)).add(item.time,'m').format('YYYY-MM-DD HH:mm');
            that.currentDate=new Date(that.DelayTime);
        },
        onOpenTime(){
            this.IsDelayTime=true;
        },
        onCancel(){
            this.IsDelayTime=false;
        },
        onConfirm(day) {
            var that = this;
            var nowdate=moment(day).format('YYYY-MM-DD HH:mm');
            that.DelayTime = nowdate;
            const date1=moment(that.TaskEndTime);
            const date2=moment(that.DelayTime);  
            that.DelayTimeDiff=date2.diff(date1,'m');
            that.timeModel={},
            that.onCancel();
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //开始日期
                    var btn = document.querySelector('#startdate');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            if(_this.form.startTime){
                                _self.picker.setSelectedValue(_this.form.startTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                _this.form.startTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            if(_this.form.startTime){
                                _self.picker.setSelectedValue(_this.form.startTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.startTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                    //结束日期
                    var btnEnd = document.querySelector('#enddate');
                    btnEnd.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            if(_this.form.endTime){
                                _self.picker.setSelectedValue(_this.form.endTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                _this.form.endTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            if(_this.form.endTime){
                                _self.picker.setSelectedValue(_this.form.endTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.endTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                });
            })(mui, document);
        },
        doComplete(item){
            var that = this;
            if(item.completeCount!=item.count){
                api.toast({
                    msg: '存在未完成的工作点，请完成所有工作点后再进行提交。',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.confirm({
                title: '提示',
                msg: '完成任务后不能再修改工作记录，如需修改需要使用重新执行功能。确认要完成工作任务吗？',
                buttons: ['确定', '取消']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        //Type-类型：1-设备 3-安防巡更 4-环境保洁
                        var options = { action: 'taskcomplete', ID: item.ID, Type: 4 };
                        ns.post(options, function (succeed, data, err) {
                            if (succeed) {
                                if (data.status) {
                                    that.get_data();
                                }
                            } else {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        })
                    }
                }
            });
        },
    },
    
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;

    app.getSearchParams();
    app.form.ProjectID=api.pageParam.ProjectID || 0;
    app.ProjectName=api.pageParam.ProjectName || '';
    app.IsAnalysis=api.pageParam.IsAnalysis || false;
    var startMonth=api.pageParam.startMonth || '';
    if(startMonth){
      app.form.startTime=moment(startMonth).startOf("month").format("YYYY-MM-DD");
      app.form.endTime=moment(startMonth).endOf("month").format("YYYY-MM-DD");
    }
    else{
        app.doClickDay(3);  
    }
    app.form.ProjectIDs=[];
    app.form.ProjectIDs.push(app.form.ProjectID);
    app.doSelectStatus(0);

    setTimeout(() => {
        app.doSearch();
    }, 500);
    ns.init_pull();
};

